Repository navigation
bootutil: add ECIES-P384 image encryption - #2973
JayToltTech wants to merge 9 commits into
Conversation
e6338ad to
b9eaa38
Compare
b9eaa38 to
eafb3aa
Compare
The PSA ECDSA path never calls psa_crypto_init(). Ed25519, the encryption code and the FIH delay RNG all init PSA themselves, ECDSA just assumes someone else did. On Zephyr the mbedtls module's SYS_INIT usually has, so it doesn't show. With CONFIG_MBEDTLS_INIT=n, or a port that doesn't init PSA before boot_go(), psa_import_key() returns PSA_ERROR_BAD_STATE and nothing ECDSA-signed validates. Same on Mbed TLS 3.6 and 4.1. psa_crypto_init() is a no-op when PSA is already up. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
init_crypto() called psa_crypto_init() before every boot, which assumed the code under test would never need to do it itself. That's what hid the missing init in the PSA ECDSA path. Without the previous commit, sig-ecdsa-psa fails 12 of 31 core tests on both 3.6 and mbedtls-v4. The external RNG enable stays, that's harness setup. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
d91908f to
117b32e
Compare
bootutil already verifies ECDSA P-384 signatures with a SHA-384 image hash (MCUBOOT_SIGN_EC384) on the PSA Crypto backend, and the simulator covers it with the sig-p384 feature, but the Zephyr port has no way to select it. Add CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384. It selects the PSA backend, the SHA-384 image hash and the P-384 public-key PSA wants. The tinycrypt, CC310 and legacy Mbed TLS ECDSA backends are P-256 only, so no implementation choice is offered. The default key file is root-ec-p384.pem, which CMake already lists as a debug key, and keys.c picks up the ecdsap384_pub_key array that imgtool getpub emits for a P-384 key. Also add a SHA-384 counterpart to the PSA hash dependency symbols, a TLV size estimate for the trailer check (216 bytes: SHA-384 hash, a SHA-384 key hash and a DER signature of up to 104 bytes), and a twister build on nrf52840dk. P-384 is about 5 KB larger than P-256 on PSA and does not fit the default 48 KB MCUboot partition, so the test uses the existing 72 KB "big" overlay. Select MBEDTLS_ENABLE_HEAP with the built-in Mbed TLS, as BOOT_RSA_PSA does, and default its size to 8192. TF-PSA-Crypto allocates during psa_crypto_init() and the P-384 verify, and boot/zephyr/prj.conf leaves the libc malloc arena at 0, so without the heap every image is rejected. On an nRF5340 DK, 512 and 4096 bytes both fail and 8192 boots. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
An image signed with ECDSA P-384 can only be encrypted with ECIES-P256 or ECIES-X25519 today, so the key exchange and its SHA-256 KDF sit below the security level of the signature. CNSA 1.0 and FIPS 186-5 pair P-384 with SHA-384, and expect the key establishment to use the same curve. Add ECIES-P384 as MCUBOOT_ENCRYPT_EC384 with a new TLV, IMAGE_TLV_ENC_EC384 (0x35). It follows ECIES-P256 exactly except for the curve and the hash: an uncompressed 97-byte ephemeral secp384r1 point, then a 48-byte HMAC-SHA384 tag, then the AES-CTR wrapped key. HKDF also uses SHA-384 and the same "MCUBoot_ECIES_v1" info string. SHA-384 is fixed for this key exchange rather than a choice, so 0x35 identifies the whole scheme the way 0x32 does for ECIES-P256. Only the PSA backend implements it. The key agreement there is already curve-agnostic, so encrypted_psa.c needs the curve family, scalar length and PKCS#8 curve OID for P-384, and shares parse_priv_enckey() with P-256. encrypted.c keeps out of the way the same way it does for the PSA ED25519 and ECDSA builds. The TLV joins the unprotected allow-list, and BOOTUTIL_CAP_ENC_EC384 lets the simulator detect it. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
imgtool already insists that a P-384 signing key is paired with a P-384 encryption key, but ecies_hkdf() has no P-384 branch, so such an image falls through to the X25519 path and fails. Encrypt with ECIES-P384 when the --encrypt key is a P-384 key and emit the ENCEC384 TLV (0x35): ECDH on secp384r1, HKDF and HMAC on SHA-384, and the ephemeral key as an uncompressed X9.62 point. --hmac-sha accepts 384, which is the only value ECIES-P384 takes, and auto selects it. Add enc-ec384-priv.pem and enc-ec384-pub.pem as test keys alongside the other enc-* keys, and tests that decrypt the result independently with the cryptography package and check the HMAC choices are enforced. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
117b32e to
c2b00d2
Compare
Enable image encryption for CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384, mapping it to ECIES-P384 the way P-256 maps to ECIES-P256 and Ed25519 to ECIES-X25519. CONFIG_BOOT_ENCRYPT_IMAGE selects the hidden BOOT_ENCRYPT_EC384, which builds encrypted_psa.c and pulls in the PSA ECDH, HKDF, HMAC and AES-CTR wants on P-384. The default key file is enc-ec384-priv.pem, and the trailer size check accounts for the 161/177-byte TLV when the encrypted TLV is saved. The ECIES-P384 dependencies also select MBEDTLS_ECP_NIST_OPTIM with the built-in Mbed TLS, as BOOT_ECDSA_MBEDTLS does for P-256. Zephyr only offers it once an ECC key pair type is wanted, which ECIES brings in. On an nRF5340 DK it costs 1.2 KB and cuts the P-384 verify from about 14.5 s to 1.7 s, and the in-place serial recovery decrypt from 10-18 s to about 4 s. Also add a twister build of it on nrf52840dk with the "big" overlay, 68.7 KB of 72 KB. It doesn't fit the MCUboot partition of nrf5340dk. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Add enc-ec384 and enc-aes256-ec384 features. ECIES-P384 is implemented only in encrypted_psa.c, so they require sig-ecdsa-psa, sig-p384 and mbedtls-v4, and build.rs refuses any other combination. The TLV generator learns ENCEC384 (ECDH P-384, HKDF and HMAC on SHA-384), the images are signed with P-384, and the embedded decryption key is the PKCS#8 form of enc-ec384-priv.pem. CI gains three rows, AES-128, AES-256 and swap-offset with primary slot validation, one per job because together they ran 5h54m against the 6h job limit. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Document the ECIES-P384 key exchange and its 0x35 TLV in encrypted_images.md and the TLV list in design.md, and add a release note. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
boot_decrypt_key() cleared the private key, the derived key and the IV/key buffer with memset() just before they go out of scope, which the compiler may drop as dead stores. Use bootutil_wipe_memory() for these, and for the caller's enckey on failure. Two error paths returned without clearing anything: a failed parse_priv_enckey(), which can leave a partial private key on the stack, and a failed psa_key_derivation_output_bytes(), which can leave a partial derived key. Both now wipe the buffer before returning. Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
c2b00d2 to
d24d861
Compare
|
Ran this on an nRF5340 DK (
The branch now also selects FYI for anyone testing recovery with encryption: recovery answers the last upload chunk and only then decrypts. A host that resets the device as soon as the upload returns gets a slot full of ciphertext and an "Image in the primary slot is not valid!" on the next boot. Wait for the device to answer an SMP request again before resetting. I tripped over that myself first. |
Depends on #2972 and, through it, #2969. The first three commits here are theirs.
Signing with P-384 today leaves you encrypting with ECIES-P256 or X25519 and a SHA-256 KDF, which is weaker than the signature. CNSA 1.0 and FIPS 186-5 pair P-384 with SHA-384 and want the key establishment on the same curve. imgtool already insists on a P-384 encryption key when you sign with P-384 (
main.py), butecies_hkdf()has no P-384 branch, so it falls into the X25519 path and dies. #2889 goes the other way and lets a P-256 encryption key ride along with a P-384 signature. This is the fix I'd rather have.The scheme is ECIES-P256 with the curve and the hash swapped. 97-byte uncompressed ephemeral point, 48-byte HMAC-SHA384 tag, then the AES-CTR wrapped key, so 161 or 177 bytes depending on AES key size. HKDF is SHA-384 with no salt and the same
MCUBoot_ECIES_v1info. SHA-384 isn't optional here, so the TLV identifies the whole scheme the way0x32does for P-256 and there's no SHA-256 variant to allocate later.On the TLV value.
0x35is the next one afterENC_X25519_SHA512and I couldn't find an open PR or issue using it. We've had this in product from our fork since August, so there are units in the field reading0x35as ECIES-P384. I'd ask that it keep that value.Only PSA implements it.
encrypted_psa.calready did the ECDH throughpsa_key_derivation_key_agreement(), which doesn't care about the curve, so the change there is the curve family, scalar length and PKCS#8 curve OID, and P-256 and P-384 shareparse_priv_enckey(). On Zephyr,CONFIG_BOOT_ENCRYPT_IMAGEwith the P-384 signature type selects it, same as P-256 maps to ECIES-P256 and Ed25519 to X25519. It also turns onMBEDTLS_ECP_NIST_OPTIM, asBOOT_ECDSA_MBEDTLSdoes for P-256. Zephyr only offers that once an ECC key pair type is wanted, which ECIES brings in, so #2972 can't do the same for a verify-only build. It costs 1.2 KB and takes the P-384 verify on an nRF5340 DK from about 14.5 s to 1.7 s.Commits:
--hmac-sha 384, test keys, testsMBEDTLS_ECP_NIST_OPTIMwith the built-in Mbed TLSenc-ec384andenc-aes256-ec384(needsig-ecdsa-psa sig-p384 mbedtls-v4) plus three CI rows, one job eachTested:
sig-ecdsa-psa sig-p384 enc-ec384 mbedtls-v4passes all 31 with--test-threads=1, which is howci/sim_run.shruns PSA rows. Run in parallel, it fails a few tests at random, and so does main withsig-ecdsa-psa sig-p384 mbedtls-v4orsig-ecdsa-psa enc-ec256 mbedtls-v4. Switching the sim's TLV generator to HKDF-SHA256 makes 12 of them fail on "MAC verification failed", so the decrypt path really is being exercised. The AES-256 and swap-offset/validate-primary-slot/max-align-16 variants pass too, as do the existing rows I could think of that this touches:sig-ecdsa-psawithsig-p384and withenc-ec256/enc-aes256-ec256(3.6 andmbedtls-v4),sig-ecdsa enc-ec256,sig-ecdsa-mbedtls enc-ec256-mbedtls,enc-ec256,enc-x25519,enc-aes256-x25519,sig-ed25519 enc-x25519,enc-rsa,enc-kwandcustom-enc-crypto.tests/test_encryption.pydecrypts the image with the cryptography package independently of imgtool, for AES-128 and AES-256, and checks the--hmac-shacombinations are refused. The rest of the suite passes excepttest_keygen_type[ecdsa-p256]and[ecdsa-p384], which fail the same way on main with OpenSSL 4.1 here.CONFIG_MCUBOOT_USE_TLV_ALLOW_LIST=y, 124 KB of 132 KB.CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P384andCONFIG_BOOT_ENCRYPT_IMAGEon top of the defaults (126.1 KB). I uploaded an ECIES-P384 image through serial recovery. It was AES-128 with a 22372-byte body, so not a multiple of 16. The image decrypted in place and booted, and slot 0 matched an independent decrypt byte for byte. Boot, including the verify, finishes at 1.97 s. The decrypt runs after recovery has acknowledged the last chunk and takes about 4 s (10 to 18 s withoutNIST_OPTIM). A host that resets the board straight after the upload leaves ciphertext in slot 0, and the board goes back into recovery. That's how recovery already works, not something this PR changes.boot_decrypt_key()recover the golden AES key from the same0x35TLV.Thoughts?